﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqAndLambda.Modules
{
    public class 基本语法
    {
        public static void Jiben()
        {
            var productList = GetProducts();

            var product = from pro in productList
                              //where pro.Price > 100     where 条件
                              //orderby pro.Price descending  升序降序
                          select pro;

            //分组 + 排序
            var groupList = from pro in productList
                            group pro by pro.CategoryId into groupData
                            orderby groupData.Count() ascending
                            select groupData;
            //自定义类型数据
            var list = from pro in productList
                          select new { pro.ProductName, ProId=pro.ProductId,Remark="备注信息" };

            foreach (var item in product)
            {
                Console.WriteLine($"Id:{item.ProductId} - 名称:{item.ProductName} - 价格:{item.Price}");
            }

            Console.WriteLine("------------------------------------------");

            foreach (var item in groupList)
            {
                Console.WriteLine($"{item.Key}.");
                foreach (var pro in item)
                {
                    Console.WriteLine($" 名称:{pro.ProductName}");
                }
            }

            Console.WriteLine("------------------------------------------");
            foreach (var item in list)
            {
                Console.WriteLine($"名称:{item.ProductName},Id:{item.ProId},备注:{item.Remark}");
            }

        }

        public static List<Category> GetCategories()
        {
            List<Category> list = new List<Category>(){
            new Category(){ CategoryId=1, CategoryName="服装" },
            new Category(){ CategoryId=2, CategoryName="食品" },
            new Category(){ CategoryId=3, CategoryName="办公" }
        };
            return list;
        }

        public static List<Product> GetProducts()
        {
            List<Product> list = new List<Product>(){
            new Product(){ ProductId=1,  ProductName="雪中飞羽绒服" , Price=998, Storage=588, CategoryId=1},
            new Product(){ ProductId=2, ProductName="安踏鞋子" , Price=198, Storage=288, CategoryId=1},
            new Product(){ ProductId=3, ProductName="短袖", Price=95, Storage=88, CategoryId=1 },
            new Product(){ ProductId=4, ProductName="旺旺雪饼" ,Price=16.5, Storage=18, CategoryId=2  },
            new Product(){ ProductId=5, ProductName="汇源果汁" ,Price=6, Storage=12, CategoryId=2 },
            new Product(){ ProductId=6, ProductName="英雄钢笔"  ,Price=12.5, Storage=59, CategoryId=3 }
        };
            return list;
        }
    }


}


